package com.xbongbong.paas.group;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.config.constant.ListGroupConstant;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasListGroupEntity;
import com.xbongbong.paas.domain.entity.ext.PaasListGroupEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasListGroupModel;
import com.xbongbong.paas.pojo.ListGroupSortPoJo;
import com.xbongbong.paas.pojo.dto.ListGroupAddDTO;
import com.xbongbong.paas.pojo.dto.ListGroupListDTO;
import com.xbongbong.paas.pojo.dto.ListGroupSortDTO;
import com.xbongbong.paas.pojo.vo.GroupListVO;
import com.xbongbong.paas.pojo.vo.ListGroupAddVO;
import com.xbongbong.paas.pojo.vo.ListGroupSortVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.GroupErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.ListGroupEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/11/05 14:48
 */
@Component
@Slf4j
public abstract class AbstractProListGroupService implements ProListGroupService {

    private static final Integer GROUP_NAME_MAX_NUM = 20;

    /**
     * 除了noEditCustom和noEditSubCustom,没有All的列表
     */
    private static final List<Integer> NO_ALL_GROUP = Arrays.asList(XbbRefTypeEnum.CUSTOMER_NEARBY.getCode());
    @Resource
    private CommonHelp commonHelp;


    @Resource
    private PaasListGroupModel paasListGroupModel;
    @Resource
    private PackageHelp packageHelp;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;

    @Override
    public GroupListVO list(ListGroupListDTO groupListDTO) throws XbbException {
        GroupListVO groupListVO = new GroupListVO();
        if (ListGroupConstant.DISABLE_SUB_CUSTOM.contains(groupListDTO.getSubBusinessType())){
            groupListVO.setShowSetting(false);
            groupListVO.setListGroupList(new ArrayList<>());
            return groupListVO;
        }
        // 是否禁用分组设置
        boolean showSetting = showSetting(groupListDTO);
        // 新版工作流开启 而且 业务支持审批时展示【审批中】分组
        boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(groupListDTO.getCorpid());
        boolean supportApproval = !Objects.equals(groupListDTO.getDistributorMark(), DistributorMarkEnum.DISTRIBUTOR.getCode()) && isOpenWorkflow && XbbRefTypeEnum.supportApprovalBusiness().contains(groupListDTO.getBusinessType());
        List<PaasListGroupEntity> list = new ArrayList<>();
        if (!Objects.equals(groupListDTO.getSubBusinessType(),XbbRefTypeEnum.STOCK_SEARCH.getCode())){
            list = paasListGroupModel.listByUserId(groupListDTO.getCorpid(), groupListDTO.getAppId(), groupListDTO.getFormId(), groupListDTO.getUserId(), "sort asc");
            if (list == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.LIST_GROUP_NOT_EXIST);
            }
        }
        if (Objects.equals(SaasMarkEnum.SAAS.getCode(), groupListDTO.getSaasMark())) {
            list = getSaasListGroup(groupListDTO, list, showSetting, supportApproval);
        } else if (Objects.equals(SaasMarkEnum.PAAS.getCode(), groupListDTO.getSaasMark())) {
            list = getPaasListGroup(groupListDTO, list, isOpenWorkflow);
        }
        //
        List<PaasListGroupEntityExt> resultList = getListGroup(list, groupListDTO);
        // 根据权限移除掉不合适的分组
        resultList = removeByPermission(groupListDTO, resultList);
        groupListVO.setListGroupList(resultList);
        groupListVO.setShowSetting(showSetting);
        setSaasSwimLaneView(groupListVO, groupListDTO);
        return groupListVO;
    }


    @Override
    public ListGroupAddVO add(ListGroupAddDTO listGroupAddDTO) throws XbbException {
        ListGroupAddVO listGroupAddVO = new ListGroupAddVO();
        try {
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid", listGroupAddDTO.getCorpid());
            map.put("appId", listGroupAddDTO.getAppId());
            map.put("formId", listGroupAddDTO.getFormId());
            map.put("creatorId", listGroupAddDTO.getUserId());
            map.put("orderByStrGroup", "sort asc");
            map.put("del",0);
            Integer count = paasListGroupModel.getEntitysCount(map);
            if (count == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            if (count >= GROUP_NAME_MAX_NUM){
                throw new XbbException(GroupErrorCodeEnum.API_ERROR_351003, GroupErrorCodeEnum.API_ERROR_351003.getMsg());
            }
            List<PaasListGroupEntity> paasListGroupEntityList = new ArrayList<>();
            boolean saasHasNoDefault = count == 0;
            if (saasHasNoDefault){
                boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(listGroupAddDTO.getCorpid());
                paasListGroupEntityList = getListGroupList(listGroupAddDTO, isOpenWorkflow, count);
                if (!paasListGroupEntityList.isEmpty()){
                    paasListGroupModel.insertBatch(paasListGroupEntityList);
                }
            }
            count = count + paasListGroupEntityList.size();
            map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid",listGroupAddDTO.getCorpid());
            map.put("formId",listGroupAddDTO.getFormId());
            map.put("creatorId",listGroupAddDTO.getUserId());
            map.put("del",0);
            map.put("name",listGroupAddDTO.getName());
            Integer repeatCount = paasListGroupModel.getEntitysCount(map);
            if (repeatCount > 0){
                throw new XbbException(GroupErrorCodeEnum.API_ERROR_351001, GroupErrorCodeEnum.API_ERROR_351001.getMsg());
            }
            PaasListGroupEntity entity = new PaasListGroupEntity();
            BeanUtil.copyProperties(listGroupAddDTO, entity);
            entity.setJsonData(JSONArray.toJSONString(listGroupAddDTO.getData()));
            entity.setSort(++count);
            entity.setCreatorId(listGroupAddDTO.getUserId());
            entity.setDefaultGroupCode(-1);
            paasListGroupModel.insert(entity);

            listGroupAddVO.setListGroupId(entity.getId());
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            log.error("listGroup数据库增加列表分组出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return listGroupAddVO;
    }

    protected List<PaasListGroupEntity> getListGroupList(ListGroupAddDTO listGroupAddDTO, boolean isOpenWorkflow, Integer count) {
        List<ListGroupEnum> listGroupEnumList = ListGroupEnum.getDefaultEnum(XbbRefTypeEnum.transferBusinessType4Distributor(listGroupAddDTO.getBusinessType(), listGroupAddDTO.getDistributorMark()));
        if (Objects.equals(listGroupAddDTO.getSaasMark(), SaasMarkEnum.PAAS.getCode()) && !isOpenWorkflow) {
            listGroupEnumList.removeIf(groupItem -> Objects.equals(groupItem.getCode(), ListGroupEnum.APPROVAL.getCode()));
        }
        if(!ListGroupConstant.NO_EDIT_CUSTOM.contains(listGroupAddDTO.getBusinessType()) && !NO_ALL_GROUP.contains(listGroupAddDTO.getBusinessType()) && Objects.equals(listGroupAddDTO.getSaasMark(),SaasMarkEnum.SAAS.getCode())){
            listGroupEnumList.add(0,ListGroupEnum.ALL);
        }
        if (Objects.equals(listGroupAddDTO.getDistributorMark(), DistributorMarkEnum.OTHER.getCode()) &&
                Objects.equals(listGroupAddDTO.getSaasMark(),SaasMarkEnum.SAAS.getCode()) &&
                isOpenWorkflow && XbbRefTypeEnum.supportApprovalBusiness().contains(listGroupAddDTO.getBusinessType())) {
            listGroupEnumList.add(ListGroupEnum.APPROVAL);
        }
        List<PaasListGroupEntity> paasListGroupEntityList = new ArrayList<>();
        String userId = listGroupAddDTO.getUserId();
        for(ListGroupEnum listGroupEnum : listGroupEnumList){
            PaasListGroupEntity entity = new PaasListGroupEntity(listGroupEnum);
            entity.setId(null);
            BeanUtil.copyProperties(listGroupAddDTO, entity);
            entity.setName(listGroupEnum.getName());
            entity.setSort(++count);
            entity.setCreatorId(userId);
            paasListGroupEntityList.add(entity);
        }
        return paasListGroupEntityList;
    }

    @Override
    public ListGroupSortVO sort(ListGroupSortDTO listGroupSortDTO) throws XbbException {
        ListGroupSortVO listGroupSortVO = new ListGroupSortVO();
        try {
            List<PaasListGroupEntity> listGroupEntities = new ArrayList<>();
            Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            params.put("corpid", listGroupSortDTO.getCorpid());
            params.put("appId", listGroupSortDTO.getAppId());
            params.put("formId", listGroupSortDTO.getFormId());
            params.put("creatorId", listGroupSortDTO.getUserId());
            params.put("del", 0);
            params.put("orderByStrGroup", "sort asc");
            Integer count = paasListGroupModel.getEntitysCount(params);
//            listGroupSortDTO.getListGroupList().forEach((item)->{
            for (ListGroupSortPoJo item : listGroupSortDTO.getListGroupList()){
                PaasListGroupEntity paasListGroupEntity = new PaasListGroupEntity();
                if (count == 0){
                    paasListGroupEntity = getDefaultGroup(item, listGroupSortDTO);
                }else {
                    paasListGroupEntity.setId(item.getId());
                }
                paasListGroupEntity.setSort(item.getSort());
                long now = DateTimeUtil.getInt();
                paasListGroupEntity.setUpdateTime(now);
                listGroupEntities.add(paasListGroupEntity);
            }
            if (count == 0){
                paasListGroupModel.insertBatch(listGroupEntities);
            }else {
                params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                params.put("corpid",listGroupSortDTO.getCorpid());
                params.put("idIn", listGroupEntities);
                List<PaasListGroupEntity> listGroupList = paasListGroupModel.list(params);
                if(listGroupList == null || listGroupList.size() != listGroupEntities.size()){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.LIST_GROUP_NOT_EXIST);
                }
                paasListGroupModel.sort(listGroupEntities, listGroupSortDTO.getCorpid());
            }
        } catch (XbbException e){
            throw e;
        } catch (Exception e) {
            log.error("listGroup.sort获取当前表单的分组排序出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return listGroupSortVO;
    }

    protected PaasListGroupEntity getDefaultGroup(ListGroupSortPoJo item, ListGroupSortDTO listGroupSortDTO) {
        ListGroupEnum listGroupEnum = ListGroupEnum.getByCode(item.getId().intValue());
        if (listGroupEnum == null || Objects.equals(ListGroupEnum.UNKONW, listGroupEnum)){
            return null;
        }
        PaasListGroupEntity paasListGroupEntity = new PaasListGroupEntity(listGroupEnum);
        paasListGroupEntity.setId(null);
        BeanUtil.copyProperties(listGroupSortDTO,paasListGroupEntity);
        paasListGroupEntity.setCreatorId(listGroupSortDTO.getUserId());
        return paasListGroupEntity;
    }

    protected List<PaasListGroupEntityExt> removeByPermission(ListGroupListDTO groupListDTO, List<PaasListGroupEntityExt> extList) {
        //访客计划数据权限只有本人的，去除下属tab
        if (Objects.equals(groupListDTO.getBusinessType(),XbbRefTypeEnum.COMMUNICATE_PLAN.getCode())){
            Map<String, Integer> dataPermissionDetail = groupListDTO.getLoginUser().getDataPermissionDetail();
            Integer dataPermission = dataPermissionDetail.get("1_601") != null ? dataPermissionDetail.get("1_601") : 0;
            if (Objects.equals(dataPermission, BasicConstant.ONE)){
                extList = extList.subList(0,2);
            }
        }
        return extList;
    }

    protected List<PaasListGroupEntity> getPaasListGroup(ListGroupListDTO groupListDTO, List<PaasListGroupEntity> list, boolean isOpenWorkflow) {
        boolean isDataTrash = Objects.equals(groupListDTO.getDataTrash(), 1);

        if (list.isEmpty()){
            list.add(0, new PaasListGroupEntityExt(ListGroupEnum.ALL));
            if (!isDataTrash && isOpenWorkflow) {
                list.add(new PaasListGroupEntityExt(ListGroupEnum.APPROVAL));
            }
        } else {
            if (!isDataTrash && isOpenWorkflow && list.stream().noneMatch(item -> Objects.equals(item.getDefaultGroupCode(), ListGroupEnum.APPROVAL.getCode()))) {
                Integer maxSort = list.get(list.size() - 1).getSort();
                PaasListGroupEntity approvalGroup = addExtraDefaultGroup(groupListDTO, ListGroupEnum.APPROVAL, maxSort);
                list.add(approvalGroup);
            }
        }
        return list;
    }

    protected List<PaasListGroupEntity> getSaasListGroup(ListGroupListDTO groupListDTO, List<PaasListGroupEntity> list, boolean showSetting, boolean supportApproval) {
//是否存在默认分组,其实初始数据肯定是空的,直接塞默认分组就行,因为初始化逻辑,有自定义分组就一定有默认分组
        boolean hasDefaultGroup = false;
        for (PaasListGroupEntity entity : list){
            if (entity.getDefaultGroupCode() >= 0){
                hasDefaultGroup = true;
                break;
            }
        }
        if (!hasDefaultGroup) {
            list = getDefaultGroupList(groupListDTO, list, showSetting, supportApproval);
        } else {
            // 其他的默认分组已经入库了，新加的默认分组【审批中】在这里入库
            if (supportApproval && list.stream().noneMatch(item -> Objects.equals(item.getDefaultGroupCode(), ListGroupEnum.APPROVAL.getCode()))) {
                Integer maxSort = list.get(list.size() - 1).getSort();
                PaasListGroupEntity approvalGroup = addExtraDefaultGroup(groupListDTO, ListGroupEnum.APPROVAL, maxSort);
                list.add(approvalGroup);
            }
        }
        return list;
    }

    protected List<PaasListGroupEntity> getDefaultGroupList(ListGroupListDTO groupListDTO, List<PaasListGroupEntity> list, boolean showSetting, boolean supportApproval) {
        Integer businessType = groupListDTO.getBusinessType();
        Integer subBusinessType = groupListDTO.getSubBusinessType();
        Integer distributorMark = groupListDTO.getDistributorMark();
        Integer finalBusinessType = null;
        if (Objects.equals(ListGroupEnum.getByCode(groupListDTO.getFormId().intValue()).getRefType(), XbbRefTypeEnum.GUARANTEE_SEARCH.getCode()) || Objects.equals(ListGroupEnum.getByCode(groupListDTO.getFormId().intValue()).getRefType(), XbbRefTypeEnum.WARNING_SEARCH.getCode())) {
            finalBusinessType = ListGroupEnum.getByCode(groupListDTO.getFormId().intValue()).getRefType();
        } else {
            finalBusinessType = getFinalBusinessType(businessType,subBusinessType);
        }
        List<ListGroupEnum> listGroupEnumList = ListGroupEnum.getDefaultEnum(XbbRefTypeEnum.transferBusinessType4Distributor(finalBusinessType, distributorMark));
        List<PaasListGroupEntityExt> defaultGroupList = new ArrayList<>();
        //特殊列表页，没有分组的和禁止编辑分组的，后端不传all
        if(showSetting){
            defaultGroupList.add(new PaasListGroupEntityExt(ListGroupEnum.ALL));
        }
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_ORDER.getCode()) && Objects.equals(groupListDTO.getFormId(), 0L)) {
            //工单全部列表增加全部分组
            defaultGroupList.add(new PaasListGroupEntityExt(ListGroupEnum.ALL));
        }
        if (Objects.equals(businessType, XbbRefTypeEnum.WORK_REPORT.getCode()) && Objects.equals(groupListDTO.getFormId(), 0L)) {
            //工作报告全部列表增加全部分组
            defaultGroupList.add(new PaasListGroupEntityExt(ListGroupEnum.ALL));
        }
        if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_STATEMENT_STATISTIC.getCode())) {
            defaultGroupList.add(new PaasListGroupEntityExt(ListGroupEnum.ALL));
            if (Objects.equals(groupListDTO.getDistributorMark(), DistributorMarkEnum.OTHER.getCode())) {
                listGroupEnumList.clear();
            }
        }
        for (ListGroupEnum groupEnum : listGroupEnumList) {
            defaultGroupList.add(new PaasListGroupEntityExt(groupEnum));
        }
        if (supportApproval) {
            defaultGroupList.add(new PaasListGroupEntityExt(ListGroupEnum.APPROVAL));
        }
        list.addAll(0, defaultGroupList);
        return list;
    }

    /**
     * 是否禁用分组设置
     * @param groupListDTO
     * @return false 不禁用；true：禁用
     */
    protected boolean showSetting(ListGroupListDTO groupListDTO) {
        boolean showSetting = !ListGroupConstant.NO_EDIT_CUSTOM.contains(groupListDTO.getBusinessType()) && !ListGroupConstant.NO_EDIT_SUB_CUSTOM.contains(groupListDTO.getSubBusinessType());
        if ((Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.WORK_ORDER.getCode()) && Objects.equals(groupListDTO.getFormId(), 0L))
                || Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.STOCK_FLOW_BILL.getCode())
                || Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.BATCH_FLOW_BILL.getCode())
                || Objects.equals(groupListDTO.getSubBusinessType(), XbbRefTypeEnum.SEQ_SEARCH.getCode())
                || Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.STOCK_STANDING_BOOK.getCode())
                || Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.CONTRACT_COMPARE_RESULT.getCode())
        ) {
            //工单全部列表禁用分组设置
            showSetting = false;
        }
        if (Objects.equals(groupListDTO.getBusinessType(), XbbRefTypeEnum.WORK_REPORT.getCode()) && Objects.equals(groupListDTO.getFormId(), 0L)) {
            //工作报告全部列表禁用分组设置
            showSetting = false;
        }
        return showSetting;
    }

    protected List<PaasListGroupEntityExt> getListGroup(List<PaasListGroupEntity> list, ListGroupListDTO groupListDTO) {
        // 是否Paas表单数据回收站
        boolean isDataTrash = Objects.equals(groupListDTO.getDataTrash(), 1);
        List<PaasListGroupEntityExt> extList = new ArrayList<>();
        //子业务类型的处理
        for (PaasListGroupEntity entity : list){
            PaasListGroupEntityExt entityExt = new PaasListGroupEntityExt();
            BeanUtil.copyProperties(entity,entityExt);
            Integer code = entity.getDefaultGroupCode();
            if ((code != null && code >= 0)){
                if (ListGroupEnum.delCusFilterPublic(groupListDTO.getSubBusinessType(),code)){
                    continue;
                }
                if (ListGroupEnum.delClueFilterPublic(groupListDTO.getSubBusinessType(),code)){
                    continue;
                }
                // Paas表单数据回收站不显示审批中分组
                if (Objects.equals(code, ListGroupEnum.APPROVAL.getCode()) && isDataTrash && Objects.equals(SaasMarkEnum.PAAS.getCode(),groupListDTO.getSaasMark())) {
                    continue;
                }
                entityExt.setDefaultGroup(1);
                ListGroupEnum listGroupEnum = ListGroupEnum.getByCode(code);
                String alias = listGroupEnum == null ? "" : listGroupEnum.getAlias();
                entityExt.setDefaultAlias(alias);
            }
            extList.add(entityExt);
        }
        return extList;
    }

    /**
     * 同一个businessType可能对应不同的分组，比如产品列表和库存查询列表，根据subBusinessType区分
     * @param businessType
     * @param subBusinessType
     * @return
     */
    protected Integer getFinalBusinessType(Integer businessType,Integer subBusinessType) {
        if (subBusinessType != null) {
            if (Objects.equals(businessType,XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) || Objects.equals(businessType,XbbRefTypeEnum.SALES_OPPORTUNITY.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.GUARANTEE_SEARCH.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.WARNING_SEARCH.getCode()) || Objects.equals(businessType,XbbRefTypeEnum.CLUE.getCode())){
                return businessType;
            }
            return subBusinessType;
        }
        return businessType;
    }

    /**
     * 添加额外的默认分组
     *
     * @param groupListDTO 入参
     * @param listGroupEnum 需要添加的默认分组
     * @param maxCount 当前分组的最大排序值
     * @return PaasListGroupEntity 添加的分组
     */
    protected PaasListGroupEntity addExtraDefaultGroup(ListGroupListDTO groupListDTO, ListGroupEnum listGroupEnum, Integer maxCount) {
        PaasListGroupEntity entity = new PaasListGroupEntity(listGroupEnum);
        entity.setId(null);
        BeanUtil.copyProperties(groupListDTO, entity);
        entity.setName(listGroupEnum.getName());
        entity.setSort(++maxCount);
        entity.setCreatorId(groupListDTO.getUserId());
        paasListGroupModel.insert(entity);
        return entity;
    }

    /**
     * @Author mingliang
     * @Description 设置saas表单的泳道视图按钮
     * @Date 2021/11/24 9:36
     * @param groupListVO
     * @param groupListDTO
     **/
    public void setSaasSwimLaneView(GroupListVO groupListVO, ListGroupListDTO groupListDTO) {
        try {
            groupListVO.setIsSwimLaneView(0);
            Integer distributorMark = groupListDTO.getDistributorMark();
            if (Objects.equals(distributorMark,1)){
                return;
            }
            PaasFormExplainEntity formExplainEntity = paasFormExplainModel.getByFormId(groupListDTO.getFormId(), groupListDTO.getCorpid());
            if (Objects.isNull(formExplainEntity)){
                return;
            }
            List<FieldAttrEntity> fieldAttrEntityList = JSONArray.parseArray(formExplainEntity.getExplains(), FieldAttrEntity.class);
            Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(formExplainEntity.getExplains(), null);
            Integer businessType = groupListDTO.getBusinessType();
            if (!Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)
                    && !Objects.equals(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), businessType)
                    && !Objects.equals(XbbRefTypeEnum.CLUE.getCode(), businessType)
                    && !Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(), businessType)
                    && !Objects.equals(SaasMarkEnum.PAAS.getCode(),groupListDTO.getSaasMark())) {
                return;
            }
            if (CollectionsUtil.isEmpty(fieldAttrEntityList)){
                groupListVO.setIsSwimLaneView(1);
                return;
            }
            HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
            BeanUtil.copyProperties(groupListDTO, handlerExplainDTO);
            handlerExplainDTO.setOwnerId(new ArrayList<>());
            handlerExplainDTO.setCoUserId(new ArrayList<>());
            //saas表单：高级版旗舰版有阶段视图按钮，paas表单：仅旗舰版有阶段视图
            Integer feeType = packageHelp.getFeeType(groupListDTO.getCorpid());
            // 判断阶段推进器字段可见权限
            if (Objects.equals(groupListDTO.getSaasMark(), SaasMarkEnum.SAAS.getCode())) {
                if (!Objects.equals(PackageTypeEnum.ADVANCED.getType(), feeType) && !Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType)) {
                    return;
                }
                if (!CommonHelp.ifStageOpen(businessType, explainMap, handlerExplainDTO)) {
                    return;
                }
                for (FieldAttrEntity temp : fieldAttrEntityList) {
                    String attr = temp.getAttr();
                    Integer isOpen = temp.getIsOpen();
                    if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)
                            && Objects.equals(CustomerManagementEnum.CUSTOMER_STAGE.getAttr(), attr)
                            && Objects.equals(isOpen, 1)) {
                        groupListVO.setIsSwimLaneView(1);
                        return;
                    }
                    if (Objects.equals(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), businessType)
                            && Objects.equals(SalesOpportunityEnum.SALE_STAGE.getAttr(), attr)
                            && Objects.equals(isOpen, 1)) {
                        groupListVO.setIsSwimLaneView(1);
                        return;
                    }
                    if (Objects.equals(XbbRefTypeEnum.CLUE.getCode(), businessType)
                            && Objects.equals(ClueEnum.CLUE_STAGE.getAttr(), attr)
                            && Objects.equals(isOpen, 1)) {
                        groupListVO.setIsSwimLaneView(1);
                        return;
                    }
                    if (Objects.equals(XbbRefTypeEnum.CONTRACT.getCode(), businessType)
                            && Objects.equals(temp.getFieldType(), FieldTypeEnum.STAGE_THRUSTER.getType())
                            && !ExplainUtil.isFieldNeedHide(temp, handlerExplainDTO)
                            && Objects.equals(temp.getIsOpen(), 1)) {
                        groupListVO.setIsSwimLaneView(1);
                        return;
                    }
                }
            } else if (Objects.equals(groupListDTO.getSaasMark(), SaasMarkEnum.PAAS.getCode())) {
                if (!Objects.equals(PackageTypeEnum.ULTIMATE.getType(), feeType)) {
                    return;
                }
                for (FieldAttrEntity temp : fieldAttrEntityList) {
                    if (Objects.equals(temp.getFieldType(), FieldTypeEnum.STAGE_THRUSTER.getType())
                            && !ExplainUtil.isFieldNeedHide(temp, handlerExplainDTO)
                            && Objects.equals(temp.getIsOpen(), 1)) {
                        groupListVO.setIsSwimLaneView(1);
                        return;
                    }
                }
            }
        } catch (XbbException e) {
            return;
        }
    }
}
